<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello <%=(String)session.getAttribute("username") %>! You made it!
<br>
<hr>
<%
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String url = "jdbc:mysql://localhost:3306/newsdb?useUnicode=true&characterEncoding=utf-8";
//1加载JDBC驱动      加
    try {
        Class.forName("com.mysql.jdbc.Driver");
//2建立与数据库的连接    连
        conn = DriverManager.getConnection(url, "root", "root");
//3创建操作数据库SQL的Statement对象      预
        String sql = "select * from users";
        stmt = conn.createStatement();
//4执行SQL语句，并分析执行结果             执
        rs = stmt.executeQuery(sql);
        while (rs.next())
        {
            String username=rs.getString("username");
            String password=rs.getString(2);
            System.out.println(username+"|"+password);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
//5关闭连接，释放资源       释
        if (rs!=null){
            try {
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            rs=null;
        }
        if (stmt!=null){
            try {
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            stmt=null;
        }
        if (conn!=null){
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            conn=null;
        }
    }
%>
</body>
</html>